package com.sip.stream.activity;

import android.view.View;
import com.sip.stream.ClientApp;
import com.sip.stream.R;
import com.sip.stream.base.BaseActivity;
import com.sip.stream.databinding.GbConfigLayoutBinding;
import com.sip.stream.gbstack.GS28181Result;
import com.sip.stream.gbstack.GS28181SDK;
import com.sip.stream.gbstack.GS28181SDKCallBack;
import com.sip.stream.gbstack.GS28181SDKConfig;
import com.sip.stream.helper.GS28181Helper;
import com.sip.stream.media.ZFYMediaManager;
import com.sip.stream.utils.select.OnSelectDialogListener;
import com.sip.stream.utils.select.SelectDialogUtils;
import com.sip.stream.utils.view.ISwitch;
import com.sip.stream.utils.zfy.voice.BeepTool;

import java.util.Arrays;

public class GbConfigActivity extends BaseActivity<GbConfigLayoutBinding> {

    //获取GB28181配置
    private GS28181SDKConfig config;


    @Override
    public void initPage() {
        //获取GB28181配置文件
        if (this.config == null) {
            this.config = GS28181SDK.getInstance().getConfig();
        }

        //返回
        binding.backLin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //保存GB28181配置
        binding.saveGbBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveConfig();
            }
        });

        //去读GB配置
        initGbConfig();
        //初始化录像配置
        initRecord();
        //OSD配置
        initOsdView();
        //初始化媒体配置
        initMediaConfig();
    }

    private void initGbConfig() {
        binding.ipEd.setText(this.config.getServerIP());
        binding.serverPortEd.setText(this.config.getServerPort() + "");
        binding.serverCodeEd.setText(this.config.getServerGBCode());
        binding.deviceIdEd.setText(this.config.getDeviceGBCode());
        binding.usernameEd.setText(this.config.getAccount());
        binding.passwordEd.setText(this.config.getPassword());
        binding.keepLiveEd.setText(this.config.getHeardbeat() + "");
        binding.locationRateEd.setText((this.config.getLocationRate() / 1000) + "");
        binding.regisTimeEd.setText(this.config.getRegistertime() + "");

        binding.videoChannelNameEd.setText(this.config.getVideoChannelName());
        binding.videoChannelEd.setText(this.config.getVideoChannelID());

        binding.voiceChannelNameEd.setText(this.config.getAudioChannelName());
        binding.voiceChannelEd.setText(this.config.getAudioChannelID());

        binding.alarmChannelNameEd.setText(this.config.getSosChannelName());
        binding.alarmChannelEd.setText(this.config.getSosChannelID());

        binding.localPortEd.setText(this.config.getLocalPort() + "");
        binding.rtpMtuEd.setText(this.config.getRtpNetMtu() + "");

        binding.sipTCP.setChecked(this.config.getCmdTranType() == 2 ? true : false);
        binding.streamTCP.setChecked(this.config.getStreamTranType() == 2 ? true : false);
    }


    /**
     * 保存GB28181配置
     */
    private void saveConfig(){
        //先注销再保存重新注册
        GS28181Helper.getInstance().unregister(true);

        GS28181SDKConfig.Builder builder = new GS28181SDKConfig.Builder(this.config);
        //设置平台参数
        builder.setServerParams(binding.serverCodeEd.getText().toString(), binding.ipEd.getText().toString(), Integer.parseInt(binding.serverPortEd.getText().toString()));
        //DeviceId
        builder.setDeviceGBCode(binding.deviceIdEd.getText().toString());
        //设置用户名密码
        builder.setAccountInfo(binding.usernameEd.getText().toString(),binding.passwordEd.getText().toString());
        //设置心跳
        builder.setHeardbeat(Integer.parseInt(binding.keepLiveEd.getText().toString()));
        //定位上报频率(秒)
        builder.setLocationRate(Integer.parseInt(binding.locationRateEd.getText().toString()) * 1000);
        //设置注册有效期
        builder.setRegistertime(Integer.parseInt(binding.regisTimeEd.getText().toString()));
        //设置视频通道
        builder.setVideoChannelName(binding.videoChannelNameEd.getText().toString());
        builder.setVideoChannelID(binding.videoChannelEd.getText().toString());
        //设置音频通道
        builder.setAudioChannelName(binding.voiceChannelNameEd.getText().toString());
        builder.setAudioChannelID(binding.voiceChannelEd.getText().toString());
        //报警信息
        builder.setSOSChannelName(binding.alarmChannelNameEd.getText().toString());
        builder.setSOSChannelID(binding.alarmChannelEd.getText().toString());
        //本地端口
        builder.setLocalPort(Integer.parseInt(binding.localPortEd.getText().toString()));
        //RTP MTU
        builder.setRtpNetMtu(Integer.parseInt(binding.rtpMtuEd.getText().toString()));
        //SIP 传输模式  TCP: 2  UDP: 1
        builder.setCmdTranType(binding.sipTCP.isChecked() ? 2 : 1);
        //流传输模式 TCP: 2 UDP: 0
        builder.setStreamTranType(binding.streamTCP.isChecked() ? 2 : 0);
        //上传GPS
        builder.setUploadGps(true);
        //自动应答
        builder.setAutoAnswer(true);
        //自动同步
        builder.setAutoSyncConfig(true);
        //设置音频参数
        builder.setAudioCodeID(1);
        builder.build(new GS28181SDKCallBack() {
            @Override
            public void onComplete(GS28181Result gS28181Result) {
                if (gS28181Result.isSuccess()) {
                    builder.commit(null);
                    BeepTool.playString("保存成功，开始注册");
                    GS28181Helper.getInstance().register();
                } else{
                    BeepTool.playString("保存失败");
                }
            }
        });
    }


    //更改录像配置
    private void initRecord() {
        //录像分辨率
        binding.recordSizeTv.setText(ZFYMediaManager.getInstance().getConfig().getVideoWidth() + "X" + ZFYMediaManager.getInstance().getConfig().getVideoHeight());
        binding.recordSizeFra.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectDialogUtils.showSelectListDialog(GbConfigActivity.this, Arrays.asList(SelectDialogUtils.getSupportSizeCamera2(ClientApp.clientApp)), new OnSelectDialogListener() {
                    @Override
                    public void onSelectItemBean(String result) {
                        String[] split = result.split("X");
                        if (split.length == 2) {
                            int parseInt = Integer.parseInt(split[0]);
                            int parseInt2 = Integer.parseInt(split[1]);
                            ZFYMediaManager.getInstance().getConfig().setVideoHeight(parseInt2);
                            ZFYMediaManager.getInstance().getConfig().setVideoWidth(parseInt);
                            ZFYMediaManager.getInstance().getConfig().setStreamHeight(parseInt2);
                            ZFYMediaManager.getInstance().getConfig().setStreamWidth(parseInt);
                            binding.recordSizeTv.setText(result);
                        }
                        
                    }
                });
            }
        });

        //录像比特率
        binding.recordBTLTv.setText(ZFYMediaManager.getInstance().getConfig().getVideoBitType() == 0 ? "高" : (ZFYMediaManager.getInstance().getConfig().getVideoBitType() == 1 ? "中" : "低"));
        binding.recordBTLFra.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectDialogUtils.showSelectListDialog(GbConfigActivity.this, Arrays.asList(new String[]{getString(R.string.meida_bitrate_high), getString(R.string.meida_bitrate_middle), getString(R.string.meida_bitrate_low)}), new OnSelectDialogListener() {
                    @Override
                    public void onSelectItemBean(String result) {
                        if (result.equalsIgnoreCase(getString(R.string.meida_bitrate_low))) {
                            ZFYMediaManager.getInstance().getConfig().setVideoBitType(2);
                        } else if (result.equalsIgnoreCase(getString(R.string.meida_bitrate_middle))) {
                            ZFYMediaManager.getInstance().getConfig().setVideoBitType(1);
                        } else {
                            ZFYMediaManager.getInstance().getConfig().setVideoBitType(0);
                        }
                        
                        binding.recordBTLTv.setText(ZFYMediaManager.getInstance().getConfig().getVideoBitType() == 0 ? "高" : (ZFYMediaManager.getInstance().getConfig().getVideoBitType() == 1 ? "中" : "低"));
                    }
                });
            }
        });

        //录像帧率
        binding.recordZLTv.setText(ZFYMediaManager.getInstance().getConfig().getVideoFPS() + "");
        binding.recordZLFra.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectDialogUtils.showSelectListDialog(GbConfigActivity.this, Arrays.asList(new String[]{"15", "25", "30"}), new OnSelectDialogListener() {
                    @Override
                    public void onSelectItemBean(String result) {
                        if (result.equalsIgnoreCase("15")) {
                            ZFYMediaManager.getInstance().getConfig().setVideoFPS(15);
                            ZFYMediaManager.getInstance().getConfig().setFakeVideoFPS(15);
                        } else if (result.equalsIgnoreCase("25")) {
                            ZFYMediaManager.getInstance().getConfig().setVideoFPS(25);
                            ZFYMediaManager.getInstance().getConfig().setFakeVideoFPS(25);
                        } else if (result.equalsIgnoreCase("30")){
                            ZFYMediaManager.getInstance().getConfig().setVideoFPS(30);
                            ZFYMediaManager.getInstance().getConfig().setFakeVideoFPS(30);
                        }
                        
                        binding.recordZLTv.setText(ZFYMediaManager.getInstance().getConfig().getVideoFPS() + "");
                    }
                });
            }
        });

        //分段录像
        binding.recordFDLXTv.setText(ZFYMediaManager.getInstance().getConfig().getVideoSepTime() + "分钟");
        binding.recordFDLXFra.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectDialogUtils.showSelectListDialog(GbConfigActivity.this, Arrays.asList(new String[]{"10分钟", "20分钟", "30分钟"}), new OnSelectDialogListener() {
                    @Override
                    public void onSelectItemBean(String result) {
                        if (result.equalsIgnoreCase("10分钟")) {
                            ZFYMediaManager.getInstance().getConfig().setVideoSepTime(10);
                        } else if (result.equalsIgnoreCase("20分钟")) {
                            ZFYMediaManager.getInstance().getConfig().setVideoSepTime(20);
                        } else if (result.equalsIgnoreCase("30分钟")){
                            ZFYMediaManager.getInstance().getConfig().setVideoSepTime(30);
                        }
                        
                        binding.recordFDLXTv.setText(ZFYMediaManager.getInstance().getConfig().getVideoSepTime() + "分钟");
                    }
                });
            }
        });

        //视频预录
        binding.recordSPYLTv.setText(ZFYMediaManager.getInstance().getConfig().getVideoPreTime() + "秒");
        binding.recordSPYLFra.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectDialogUtils.showSelectListDialog(GbConfigActivity.this, Arrays.asList(new String[]{"15秒", "20秒", "25秒", "30秒", "60秒"}), new OnSelectDialogListener() {
                    @Override
                    public void onSelectItemBean(String result) {
                        if (result.equalsIgnoreCase("15秒")) {
                            ZFYMediaManager.getInstance().getConfig().setVideoPreTime(15);
                        } else if (result.equalsIgnoreCase("20秒")) {
                            ZFYMediaManager.getInstance().getConfig().setVideoPreTime(20);
                        } else if (result.equalsIgnoreCase("25秒")){
                            ZFYMediaManager.getInstance().getConfig().setVideoPreTime(25);
                        } else if (result.equalsIgnoreCase("30秒")){
                            ZFYMediaManager.getInstance().getConfig().setVideoPreTime(30);
                        } else if (result.equalsIgnoreCase("60秒")){
                            ZFYMediaManager.getInstance().getConfig().setVideoPreTime(60);
                        }
                        
                        binding.recordSPYLTv.setText(ZFYMediaManager.getInstance().getConfig().getVideoPreTime() + "秒");
                    }
                });
            }
        });

        //录像编码
        binding.recordLXBMTv.setText(ZFYMediaManager.getInstance().getConfig().getVideoMinetype().equals("video/hevc") ? "H.265/HEVC" : "H.264/AVC");
        binding.recordLXBMFra.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectDialogUtils.showSelectListDialog(GbConfigActivity.this, Arrays.asList(new String[]{"H.264/AVC", "H.265/HEVC"}), new OnSelectDialogListener() {
                    @Override
                    public void onSelectItemBean(String result) {
                        if (result.equalsIgnoreCase("H.264/AVC")) {
                            ZFYMediaManager.getInstance().getConfig().setVideoMinetype("video/avc");
                        } else if (result.equalsIgnoreCase("H.265/HEVC")) {
                            ZFYMediaManager.getInstance().getConfig().setVideoMinetype("video/hevc");
                        }
                        
                        binding.recordLXBMTv.setText(ZFYMediaManager.getInstance().getConfig().getVideoMinetype().equals("video/hevc") ? "H.265/HEVC" : "H.264/AVC");
                    }
                });
            }
        });
    }


    //初始化OSD View
    private void initOsdView() {
        //初始化OSD
        binding.osdColorSwitch.setIsOpen(ZFYMediaManager.getInstance().getConfig().isOsdInvert());
        binding.osdDateSwitch.setIsOpen(ZFYMediaManager.getInstance().getConfig().isOsdTime());
        binding.osdStatusSwitch.setIsOpen(ZFYMediaManager.getInstance().getConfig().isOsdDeviceStatus());
        binding.osdGpsSwitch.setIsOpen(ZFYMediaManager.getInstance().getConfig().isOsdLocation());
        binding.osdInfoSwitch.setIsOpen(ZFYMediaManager.getInstance().getConfig().isOsdDeviceInfo());

        //OSD 反色设置
        binding.osdColorSwitch.setOnISwitchOnClickListener(new ISwitch.ISwitchOnClickListeners() {
            @Override
            public void open() {
                ZFYMediaManager.getInstance().getConfig().setOsdInvert(true);
            }

            @Override
            public void close() {
                ZFYMediaManager.getInstance().getConfig().setOsdInvert(false);
            }
        });

        //OSD 时间设置
        binding.osdDateSwitch.setOnISwitchOnClickListener(new ISwitch.ISwitchOnClickListeners() {
            @Override
            public void open() {
                ZFYMediaManager.getInstance().getConfig().setOsdTime(true);
            }

            @Override
            public void close() {
                ZFYMediaManager.getInstance().getConfig().setOsdTime(false);
            }
        });


        //OSD 设备状态
        binding.osdStatusSwitch.setOnISwitchOnClickListener(new ISwitch.ISwitchOnClickListeners() {
            @Override
            public void open() {
                ZFYMediaManager.getInstance().getConfig().setOsdDeviceStatus(true);
            }

            @Override
            public void close() {
                ZFYMediaManager.getInstance().getConfig().setOsdDeviceStatus(false);
            }
        });

        //OSD 定位信息
        binding.osdGpsSwitch.setOnISwitchOnClickListener(new ISwitch.ISwitchOnClickListeners() {
            @Override
            public void open() {
                ZFYMediaManager.getInstance().getConfig().setOsdLocation(true);
            }

            @Override
            public void close() {
                ZFYMediaManager.getInstance().getConfig().setOsdLocation(false);
            }
        });

        //OSD 设备信息
        binding.osdInfoSwitch.setOnISwitchOnClickListener(new ISwitch.ISwitchOnClickListeners() {
            @Override
            public void open() {
                ZFYMediaManager.getInstance().getConfig().setOsdDeviceInfo(true);
            }

            @Override
            public void close() {
                ZFYMediaManager.getInstance().getConfig().setOsdDeviceInfo(false);
            }
        });
    }

    //初始化媒体配置
    private void initMediaConfig() {
        //网传视频编码
        binding.gbWCSPBMTv.setText(ZFYMediaManager.getInstance().getConfig().getStreamMinetype().equals("video/hevc") ? "H.265/HEVC" : "H.264/AVC");
        binding.gbWCSPBMFra.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectDialogUtils.showSelectListDialog(GbConfigActivity.this, Arrays.asList(new String[]{"H.264/AVC", "H.265/HEVC"}), new OnSelectDialogListener() {
                    @Override
                    public void onSelectItemBean(String result) {
                        if (result.equalsIgnoreCase("H.264/AVC")) {
                            ZFYMediaManager.getInstance().getConfig().setStreamMinetype("video/avc");
                        } else if (result.equalsIgnoreCase("H.265/HEVC")) {
                            ZFYMediaManager.getInstance().getConfig().setStreamMinetype("video/hevc");
                        }
                        
                        binding.gbWCSPBMTv.setText(ZFYMediaManager.getInstance().getConfig().getStreamMinetype().equals("video/hevc") ? "H.265/HEVC" : "H.264/AVC");
                    }
                });
            }
        });

        //双码流
        binding.gbSMLSwitch.setIsOpen(ZFYMediaManager.getInstance().getConfig().isDualStream());
        binding.gbSMLSwitch.setOnISwitchOnClickListener(new ISwitch.ISwitchOnClickListeners() {
            @Override
            public void open() {
                ZFYMediaManager.getInstance().getConfig().setDualStream(true);
                
                binding.gbSMLSwitch.setIsOpen(ZFYMediaManager.getInstance().getConfig().isDualStream());
            }

            @Override
            public void close() {
                ZFYMediaManager.getInstance().getConfig().setDualStream(false);
                
                binding.gbSMLSwitch.setIsOpen(ZFYMediaManager.getInstance().getConfig().isDualStream());
            }
        });

        //网传自动录像
        binding.gbZDLXSwitch.setIsOpen(ZFYMediaManager.getInstance().getConfig().isAutoVideoWhenRemote());
        binding.gbZDLXSwitch.setOnISwitchOnClickListener(new ISwitch.ISwitchOnClickListeners() {
            @Override
            public void open() {
                ZFYMediaManager.getInstance().getConfig().setAutoVideoWhenRemote(true);
                
                binding.gbZDLXSwitch.setIsOpen(ZFYMediaManager.getInstance().getConfig().isAutoVideoWhenRemote());
            }

            @Override
            public void close() {
                ZFYMediaManager.getInstance().getConfig().setAutoVideoWhenRemote(false);
                
                binding.gbZDLXSwitch.setIsOpen(ZFYMediaManager.getInstance().getConfig().isAutoVideoWhenRemote());
            }
        });

        //动态码流
        binding.gbDTMLSwitch.setIsOpen(ZFYMediaManager.getInstance().getConfig().isDynamicRemoteStream());
        binding.gbDTMLSwitch.setOnISwitchOnClickListener(new ISwitch.ISwitchOnClickListeners() {
            @Override
            public void open() {
                ZFYMediaManager.getInstance().getConfig().setDynamicRemoteStream(true);
                
                binding.gbDTMLSwitch.setIsOpen(ZFYMediaManager.getInstance().getConfig().isDynamicRemoteStream());
            }

            @Override
            public void close() {
                ZFYMediaManager.getInstance().getConfig().setDynamicRemoteStream(false);
                
                binding.gbDTMLSwitch.setIsOpen(ZFYMediaManager.getInstance().getConfig().isDynamicRemoteStream());
            }
        });

        //网传帧率
        binding.gbWCZLTv.setText(ZFYMediaManager.getInstance().getConfig().getStreamFPS() + "");
        binding.gbWCZLFra.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectDialogUtils.showSelectListDialog(GbConfigActivity.this, Arrays.asList(new String[]{"5", "10", "15", "25", "30"}), new OnSelectDialogListener() {
                    @Override
                    public void onSelectItemBean(String result) {
                        if (result.equalsIgnoreCase("5")) {
                            ZFYMediaManager.getInstance().getConfig().setStreamFPS(5);
                        } else if (result.equalsIgnoreCase("10")) {
                            ZFYMediaManager.getInstance().getConfig().setStreamFPS(10);
                        } else if (result.equalsIgnoreCase("15")) {
                            ZFYMediaManager.getInstance().getConfig().setStreamFPS(15);
                        } else if (result.equalsIgnoreCase("25")) {
                            ZFYMediaManager.getInstance().getConfig().setStreamFPS(25);
                        } else if (result.equalsIgnoreCase("30")) {
                            ZFYMediaManager.getInstance().getConfig().setStreamFPS(30);
                        }
                        
                        binding.gbWCZLTv.setText(ZFYMediaManager.getInstance().getConfig().getStreamFPS() + "");
                    }
                });
            }
        });

        //网传比特率
        binding.gbWCBTTv.setText(ZFYMediaManager.getInstance().getConfig().getStreamBitRate() + "");
        binding.gbWCBTLra.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectDialogUtils.showSelectListDialog(GbConfigActivity.this, Arrays.asList(new String[]{"512", "1024", "2048","4096"}), new OnSelectDialogListener() {
                    @Override
                    public void onSelectItemBean(String result) {
                        if (result.equalsIgnoreCase("512")) {
                            ZFYMediaManager.getInstance().getConfig().setStreamBitRate(512);
                        } else if (result.equalsIgnoreCase("1024")) {
                            ZFYMediaManager.getInstance().getConfig().setStreamBitRate(1024);
                        } else if (result.equalsIgnoreCase("2048")) {
                            ZFYMediaManager.getInstance().getConfig().setStreamBitRate(2048);
                        } else if (result.equalsIgnoreCase("4096")) {
                            ZFYMediaManager.getInstance().getConfig().setStreamBitRate(4096);
                        }
                        binding.gbWCBTTv.setText(ZFYMediaManager.getInstance().getConfig().getStreamBitRate() + "");
                    }
                });
            }
        });
    }

}
